<template>
  <div>
    <div>{{drag?'拖拽中':'拖拽停止'}}</div>
    <!--使用draggable组件-->
    <draggable
      v-model="myArray"
      chosenClass="chosen"
      forceFallback="true"
      group="people"
      animation="1000"
      filter=".forbid"
      @start="onStart"
      @end="onEnd"
      :move="onMove"
    >
      <transition-group>
        <div
          :class="element.id==10?'item forbid':'item'"
          v-for="element in myArray"
          :key="element.id"
        >{{element.name}}</div>
      </transition-group>
    </draggable>

    <div style="height:40px"></div>
    <div class="itxst">
      <div class="col">
        <draggable v-model="myArray" handle=".mover" animation="300" @start="onStart" @end="onEnd">
          <transition-group>
            <div class="item" v-for="item in myArray" :key="item.id">
              <span class="mover">+</span>
              {{item.name}}
            </div>
          </transition-group>
        </draggable>
      </div>
    </div>
  </div>
</template>
<style scoped>
/*被拖拽对象的样式*/
.item {
  padding: 6px;
  background-color: #fdfdfd;
  border: solid 1px #eee;
  margin-bottom: 10px;
  cursor: move;
  width: 60%;
}
/*选中样式*/
.chosen {
  border: solid 1px #3089dc !important;
  background: #ccc;
}

.mover {
  background-color: #fdfdfd;
  cursor: move;
  padding: 3px 6px;
}
</style>
<script>
//导入draggable组件
import draggable from "vuedraggable";
export default {
  //注册draggable组件
  components: {
    draggable
  },
  data() {
    return {
      drag: false,
      //定义要被拖拽对象的数组
      myArray: [
        { people: "cn", id: 10, name: "www.itxst.com" },
        { people: "cn", id: 20, name: "www.baidu.com" },
        { people: "cn", id: 30, name: "www.taobao.com" },
        { people: "us", id: 40, name: "www.yahoo.com" }
      ]
    };
  },
  methods: {
    //开始拖拽事件
    onStart() {
      this.drag = true;
    },
    //拖拽结束事件
    onEnd() {
      this.drag = false;
    },
    //禁止拖动到id为10的对象
    onMove(e) {
      console.log(e.relatedContext.element.id);
      if (e.relatedContext.element.id == 10) return false;
      return true;
    }
  }
};
</script>